package cn.neusoft.dao.impl;

import cn.neusoft.bean.Food;
import cn.neusoft.bean.User;
import cn.neusoft.dao.IUserDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.FoodQuery;
import cn.neusoft.query.UserQuery;
import cn.neusoft.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao implements IUserDao {
    private QueryRunner queryRunner;

    public UserDao(){
        queryRunner = JdbcUtils.getQueryRunner();
    }
    @Override
    public int add(User user) throws SQLException {
        String sql="insert into t_user(userId, password, userName, userSex, userImg, dalTag) VALUES (?,?,?,?,?,?)";

        return queryRunner.update(sql,
                user.getUserId(),
                user.getPassword(),
                user.getUserName(),
                user.getUserSex(),
                user.getUserImg(),
                user.getDalTag());
    }

    @Override
    public int update(User user) throws SQLException {
        String sql="update t_user set password=?, userName=?, userSex=?, userImg=?, dalTag=? where userId=?";
        return queryRunner.update(sql,
                user.getPassword(),
                user.getUserName(),
                user.getUserSex(),
                user.getUserImg(),
                user.getDalTag(),
                user.getUserId());
    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql="delete from t_user where userId=?";

        return queryRunner.update(sql,id);
    }

    @Override
    public User getById(Integer id) throws SQLException {
        String sql="select userId, password, userName, userSex, userImg, dalTag from t_user where userId=?";
        return queryRunner.query(sql,new BeanHandler<User>(User.class),id);

    }

    @Override
    public List<User> getAll() throws SQLException {
        String sql="select userId, password, userName, userSex, userImg, dalTag from t_user";
        return queryRunner.query(sql,new BeanListHandler<>(User.class));

    }

    @Override
    public int getCount(BaseQuery baseQuery) throws SQLException {
        UserQuery query=(UserQuery) baseQuery;
        List<Object> params=new ArrayList<>();
        String sql="select count(userId) from t_user where 1=1";
        if(query.getUserName()!=null){
            sql+=" and userName like concat('%',?,'%')";
            params.add(query.getUserName());
        }
        System.out.println("查询总记录："+sql);
        System.out.println("参数集合："+params);
        return queryRunner.query(sql, new ScalarHandler<Long>(), params.toArray()).intValue();
    }

    @Override
    public List<User> getByPage(BaseQuery baseQuery) throws SQLException {
        UserQuery query=(UserQuery) baseQuery;
        List<Object> params=new ArrayList<>();
        String sql="select userId, password, userName, userSex, userImg, dalTag from t_user where 1=1 ";
        if(query.getUserName()!=null){
            sql+=" and UserName like concat('%',?,'%')";
            params.add(query.getUserName());
        }
        sql+="limit ?,?";
        params.add(query.getStartIndex());
        params.add(query.getPageCount());
        return  queryRunner.query(sql,new BeanListHandler<User>(User.class),params.toArray());
    }
}
